package cn.edu.flink.tutorial.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumerBase;

import java.time.Duration;
import java.util.Properties;

public class Kafka {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // kafka 配置项
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "localhost:9092");
        properties.setProperty("group.id", "consumer-group");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");
        // 动态发现分区
        properties.setProperty(FlinkKafkaConsumerBase.KEY_PARTITION_DISCOVERY_INTERVAL_MILLIS, 2 * 60 * 1000 + "");


        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<>("sensor", new SimpleStringSchema(), properties);

        // kafkaConsumer.setStartFromLatest();
        // 如果调用setStartFromLatest（），则无需在属性 Properties中放置"auto.offset.reset", "latest"。
        // 当checkpoint机制开启的时候，KafkaConsumer会定期把kafka的offset信息还有其他 operator 的状态信息一块保存起来。
        // 当 job 失败重启的时候，Flink 会从最近一次的 checkpoint 中进行恢复数据，重新从保存的 offset 消费 kafka 中的数据

        // 从 kafka 读取数据
        DataStream<String> dataStream = env.addSource(kafkaConsumer);


        // 2.打印
        dataStream.print();

        // 3.执行
        env.execute();
    }
}
